package org.exia.minisig.swing.view.navigation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.commons.lang3.*;
import org.exia.minisig.model.object.Interest;
import org.exia.minisig.model.object.Itinerary;
import org.exia.minisig.model.object.News;
import org.exia.minisig.model.object.Place;
import org.exia.minisig.services.ItineraryManager;
import org.exia.minisig.swing.controller.HomeFramMode;

@SuppressWarnings("serial")
public class InterestAddPanel extends JPanel {
 //VAR
 private NavigationFram parentFram;
 private String data[];
 private JTextField fieldURL; 
 private String data2[];
 private JList<String> listeURL;
 private JList<String> listeURLurl;
 private JTextField labelField;
 private JTextArea textArea;
 private JComboBox<String> jcomboBox;
 private JLabel fieldX;
 private JLabel fieldY;
 //CONSTRUCTOR
 public InterestAddPanel(String s, NavigationFram _parentFram){
  initializeVar(s, _parentFram);
 }
 
 public void initializeVar(String s, NavigationFram _parentFram){
	 this.setBackground(Color.white);
  this.textArea = new JTextArea();
  this.listeURLurl = new JList<>(data2);
  this.listeURL = new JList<>(data);
  this.data  = new String[]{};
  this.fieldURL = new JTextField();
  this.data2  = new String[]{};
  this.parentFram = _parentFram;
  this.labelField = new JTextField();
  ////
  fillPanel();
 }
 
 public void fillPanel(){
  this.removeAll();
  this.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  this.setLayout(new GridBagLayout());
  GridBagConstraints gc = new GridBagConstraints();
  gc.fill=GridBagConstraints.BOTH;
  //PARAMETER
  gc.weightx=1;
  gc.weighty=1;
  //---------------------------------------//
  //TITRE
  gc.gridheight=1;
  gc.gridwidth= 4;
  gc.gridx=0;
  gc.gridy=0;
  gc.insets = new Insets(0, 0, 5, 0);
  JPanel panelTitre = new JPanel(new BorderLayout());
  JLabel titre = new JLabel("Ajout " );
  
  titre.setBackground(Color.white);
	titre.setOpaque(true);
  
  titre.setVerticalAlignment(SwingConstants.CENTER);
  titre.setHorizontalAlignment(SwingConstants.CENTER);
  panelTitre.add(titre, BorderLayout.CENTER);
  this.add(panelTitre, gc);
  //--------------------------------RDC------------------------------//
  //ETAGE 01
  //label 
  gc.insets = new Insets(0, 10, 0, 0);
  gc.gridwidth = 1;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=1;
  JLabel labelLabel = new JLabel("Label :");
  labelLabel.setHorizontalAlignment(SwingConstants.CENTER);
  this.add(labelLabel, gc);
  //field
  gc.insets = new Insets(5, 10, 5, 10);
  gc.gridwidth = 3;
  gc.gridheight = 1;
  gc.gridx=1;
  gc.gridy=1;
  JPanel panelFieldLabel = new JPanel(new BorderLayout(0,0));
  
  labelField.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  labelField.setMargin(new Insets(50, 0, 0, 0));
  labelField.setBorder(BorderFactory.createCompoundBorder(
    labelField.getBorder(), 
        BorderFactory.createEmptyBorder(0, 10, 0, 10)));
  panelFieldLabel.add(labelField, BorderLayout.CENTER);
  this.add(panelFieldLabel, gc);
  
  //ETAGE TEXT LONG
  gc.insets = new Insets(3, 5, 3, 5);
  gc.gridwidth = 4;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=2;
  //TEST
  JPanel panel = new JPanel();
  panel.setLayout(new BorderLayout()); 
  JScrollPane scroll = new JScrollPane(textArea);
  panel.add(scroll, BorderLayout.CENTER);
  this.add(panel, gc);
  
  
  //ETAGE IMAGE 
  //BTN +
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=3;
  JButton btnPlus = new JButton("Ajout img");
  btnPlus.addActionListener(new ActionListener() {
   
  @Override
  public void actionPerformed(ActionEvent e) {    
   JFileChooser chooser = new JFileChooser();
      FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif","png");
      chooser.setFileFilter(filter);
      int returnVal = chooser.showOpenDialog(null);
      if(returnVal == JFileChooser.APPROVE_OPTION) {
       String y = chooser.getSelectedFile().getAbsolutePath();
       addImg(y);
      }     
  }
  });
  
  btnPlus.setFocusPainted(false);
  this.add(btnPlus, gc);
  //BTN -
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=2;
  gc.gridy=3;
  JButton btnLess = new JButton("Supprime img");
  btnLess.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    if (listeURL.getSelectedValue() != null) {
        int z= listeURL.getSelectedIndex();
        
     data = ArrayUtils.remove(data,z );
     fillPanel();
    }
   }
  });
  
  this.add(btnLess, gc);
  //ETAGE LISTE  URL IMAGE
  gc.insets = new Insets(3, 5, 3, 5);
  gc.gridwidth = 4;
  gc.gridheight = 3;
  gc.gridx=0;
  gc.gridy=4;
  
  this.listeURL = new JList<>(data);
  this.listeURL.setVisibleRowCount(5);
  JScrollPane panelListImg = new JScrollPane(this.listeURL);
  this.add(panelListImg, gc);
  //URL ETAGE BTN
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=8;
  
  this.fieldURL.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  this.fieldURL.setMargin(new Insets(50, 0, 0, 0));
  this.fieldURL.setBorder(BorderFactory.createCompoundBorder(
  this.fieldURL.getBorder(), 
  BorderFactory.createEmptyBorder(0, 10, 0, 10)));
  this.add(this.fieldURL, gc);
  //ETAGE BTN
  //+
  gc.gridwidth = 1;
  gc.gridheight = 1;
  gc.gridx=2;
  gc.gridy=8;
  JButton btnPlusURL = new JButton("Ajout");
  btnPlusURL.addActionListener(new ActionListener() {
   
  @Override
  public void actionPerformed(ActionEvent arg0) {
   String x =fieldURL.getText();
    addUrl(x);
   }
  });
  btnPlus.setFocusPainted(false);
  this.add(btnPlusURL, gc);
  //-
  gc.gridwidth = 1;
  gc.gridheight = 1;
  gc.gridx=3;
  gc.gridy=8;
  JButton btnLessURL = new JButton("Del");
  btnLessURL.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    if (listeURLurl.getSelectedValue() != null) {
        int z= listeURLurl.getSelectedIndex();
     data2 = ArrayUtils.remove(data2,z );
     fillPanel();
    }
    
   }
  });
  btnLessURL.setFocusPainted(false);
  this.add(btnLessURL, gc);
  
  //ETAGE LISTE  URL URL
  gc.insets = new Insets(3, 5, 3, 5);
  gc.gridwidth = 4;
  gc.gridheight = 3;
  gc.gridx=0;
  gc.gridy=9;
  
  this.listeURLurl = new JList<>(data2);
  this.listeURLurl.setVisibleRowCount(5);
  JScrollPane panelListUrl = new JScrollPane(this.listeURLurl);
  this.add(panelListUrl, gc);
  //----------------------------------------------------------------------------------------
  gc.gridwidth = 1;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=12;
  JLabel labelParcours = new JLabel("Parcours :");
  this.add(labelParcours, gc);
  //
  gc.gridwidth = 3;
  gc.gridheight = 1;
  gc.gridx=1;
  gc.gridy=12;
  jcomboBox = new JComboBox<>();
  jcomboBox.addItem("-------------------");
  for (Itinerary item: parentFram.getServiceItinerary().getAll()) {
		jcomboBox.addItem(item.getLabel());
	}
  this.add(jcomboBox, gc);
  //
  gc.gridwidth = 1;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=13;
  JButton setOnMap = new JButton("Placer");
  setOnMap.addActionListener(new ActionListener() {
	@Override
	public void actionPerformed(ActionEvent e) {
		//TODO
		parentFram.mapIsWaitingClic(true);
	}
});
  this.add(setOnMap, gc);
  //
  //
  gc.gridwidth = 3;
  gc.gridheight = 1;
  gc.gridx=1;
  gc.gridy=13;
  JPanel XY = new JPanel(new BorderLayout());
  this.add(XY, gc);
  //
  	  JPanel X = new JPanel(new BorderLayout());
	  JLabel xlabel = new JLabel("X :");
	  xlabel.setHorizontalAlignment(SwingConstants.CENTER);
	  xlabel.setVerticalAlignment(SwingConstants.CENTER);
	  X.add(xlabel, BorderLayout.WEST);
	  fieldX = new JLabel("");  
	  X.add(fieldX, BorderLayout.CENTER);
	  JPanel Y = new JPanel(new BorderLayout());
	  JLabel ylabel = new JLabel("Y :");
	  ylabel.setHorizontalAlignment(SwingConstants.CENTER);
	  ylabel.setVerticalAlignment(SwingConstants.CENTER);
	  Y.add(ylabel, BorderLayout.WEST);
	  fieldY = new JLabel("                       ");
	  Y.add(fieldY, BorderLayout.CENTER);
  XY.add(X, BorderLayout.WEST);
  XY.add(Y, BorderLayout.EAST);
//----------------------------------------------------------------------------------------  
  //ETAGE BTN
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=0;
  gc.gridy=14;
  JButton btnSave = new JButton("Save");
  btnSave.setFocusPainted(false);
  this.add(btnSave, gc);
  btnSave.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
   //TODO
	   parentFram.mapIsWaitingClic(false);
	   ///////////////////////////////////////
	   Interest newInterest = new Interest();
	   newInterest.setLabel(labelField.getText());
	   newInterest.setText(textArea.getText());
        for (String img: data) {
        	newInterest.addImage(img); 
        }
        for (String url: data2) {
        	newInterest.addURL(url); 
        }
        
        newInterest.setItinerary(
        		new Itinerary((String) jcomboBox.getSelectedItem())
        		);
        if (parentFram.getPositionClicked() != null){
        	newInterest.setX(
        			(int)
        			(parentFram.getPositionClicked().x / parentFram.getMap().getZoomRate())
        			);        	
           newInterest.setY(
        		    (int)
    		   		(parentFram.getPositionClicked().y / parentFram.getMap().getZoomRate())
    		   		);	
        }
        newInterest.setIdMap(parentFram.getPlace().getId());
        parentFram.getServiceInterest().addInterest(newInterest);
        parentFram.AddMode(false);
    	parentFram.reload();
    	parentFram.loadInterest();
   }
  });
  //-
  gc.gridwidth = 2;
  gc.gridheight = 1;
  gc.gridx=2;
  gc.gridy=14;
  JButton btnCancel = new JButton("Cancel");
  btnCancel.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent arg0) {
	   parentFram.mapIsWaitingClic(true);
    parentFram.reload();
   }
  });
  btnPlus.setFocusPainted(false);
  this.add(btnCancel, gc);
  this.revalidate();
 }

public void changeLabelPosition(Point p){
	this.fieldX.setText(""+p.x);
	this.fieldY.setText(""+p.y);
	this.revalidate();
}

public void addUrl(String x){
  String dataTemp[] = new String[data2.length+1]; 
  System.arraycopy(data2, 0, dataTemp, 0, data2.length);
  dataTemp[data2.length]=x;
  data2 = dataTemp;
  fillPanel();
 }
 public void addImg(String x){
  String dataTemp[] = new String[data.length+1]; 
  System.arraycopy(data, 0, dataTemp, 0, data.length);
  dataTemp[data.length]=x;
  data = dataTemp;
  fillPanel();
 }

}
